<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Slide extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->checkAdmin($this->session->userdata('admin_id'));
        $this->data['controller'] = 'slide';         
        $this->load->model('Mslide');
    }
    
    public function index()
    {
        $this->data['id_active'] = $this->getParamUri(3,2);
        $this->load->library('pagination');
        $config['per_page'] = 5;
        $this->data['page'] = $this->data['pageg'] = $this->getParamUri(3,2);
        $data = $this->Mslide->getSlideDB($config['per_page'],$this->getParamUri(3,2));
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 3;
        $config['uri_segment_page'] = $this->getParamUri(3,2);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
        $this->data['data'] = $data['data'];
        
	    $this->data['action'] = 'index';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function editslide()
    {
        $this->load->library('form_validation');
        
        $this->data['id'] = $this->getParamInt('id');
        $this->data['data'] = $this->Mslide->getSlideEdit($this->data['id']);
        
        $this->form_validation->set_rules('namePro', 'Tên slide', 'required');
        
        if($this->form_validation->run() === TRUE)
        {
            $dataimage = $this->getParamString('img_hidden');
            if(!in_array($dataimage, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'nhatnv-'.$timenow.'.png';
                $paths = PROJECT_PATH.'/public/upload/images/';
                $exp = explode("[removed]",$dataimage);
                $this->savefile($exp[1],$paths,$nameimagess);
                $img = 'public/upload/images/'.$nameimagess;
            }else{
                $img = '';
            }
        
            $run = $this->Mslide->editSlideDb($this->data['id'],$img);
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Sửa slide thành công");window.location.href="'.$this->data['baseurl'].'manage/slide.html";</script>');
            }
        }
        $this->data['action'] = 'editslide';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function sortcateslide()
    {
        $this->data['data'] = $this->Mslide->getNameSlide();
        $this->data['action'] = 'sortcateslide';
        $this->load->view('temp/layout', $this->data);
    } 
    public function savesortslide()
    {
        $id_list = $this->getParamArray('id');
        $this->Mslide->sortsaveDB($id_list);
    }
    public function addslide()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('namePro', 'Tên slide', 'required');
        $this->form_validation->set_rules('img_hidden', 'Hình ảnh', 'required');
        
        if($this->form_validation->run() === TRUE)
        {
            $dataimage = $this->getParamString('img_hidden');
            if(!in_array($dataimage, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'nhatnv-'.$timenow.'.png';
                $paths = PROJECT_PATH.'/public/upload/images/';
                $exp = explode("[removed]",$dataimage);
                $this->savefile($exp[1],$paths,$nameimagess);
                $img = 'public/upload/images/'.$nameimagess;
            }else{
                $img = '';
            }
        
            $run = $this->Mslide->addSlideDb($img);
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Thêm Slide thành công");window.location.href="'.$this->data['baseurl'].'manage/slide.html";</script>');
            }
        }
        $this->data['action'] = 'addslide';
        $this->load->view('temp/layout', $this->data);
    }
    
    private function selectType($id = 0)
    {
        $arr = array('Cốc', 'Cái', 'Bịch', 'Chục', 'Hộp', 'Thùng', 'Chiếc', 'Lô', 'Gói', 'tá', 'Kg', 'Gam');
        $html  = '';
        foreach($arr AS $el=>$le)
        {
            $ids = ($el + 1);
            if($id == $ids)
            {
                $selected = 'selected="selected"';
            }else{
                $selected = '';
            }
            $html .= '<option value="'.$ids.'" '.$selected.'>'.$le.'</option>';
        }
        return $html;
    }
    
    public function deleteslide()
    {
        $run = $this->Mslide->deleteslideDB();
        if($run == 200)
        {
            die('<meta charset="utf-8"><script>alert("Xóa slide thành công");window.location.href="'.$this->data['baseurl'].'manage/slide.html";</script>');
        }
        
    }
    
    
}
